package com.michaelbulava.TFS.tasks.workspaces;

import com.intellij.openapi.progress.ProgressIndicator;
import com.michaelbulava.TFS.Messages;
import com.michaelbulava.TFS.tasks.BackgroundableTask;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspaceLocation;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspaceOptions;
import com.microsoft.tfs.core.clients.versioncontrol.WorkspacePermissionProfile;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.WorkingFolder;
import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.Workspace;

import java.text.MessageFormat;

/**
 * Created by mbulava on 7/16/2015.
 */
public class UpdateWorkspaceTask extends BackgroundableTask {

    private final Workspace workspace;
    private final String newWorkspaceName;
    private final String newComment;
    private final WorkingFolder[] newWorkingFolders;
    private final WorkspaceOptions newOptions;
    private final WorkspaceLocation newLocation;
    private final WorkspacePermissionProfile permissionprofile;

    public UpdateWorkspaceTask(Workspace workspace, String newWorkspaceName, String newComment,
                               WorkingFolder[] newWorkingFolders, WorkspaceOptions newOptions,
                               WorkspaceLocation newLocation, WorkspacePermissionProfile permissionprofile) {
        super(null, getName(workspace), true);

        this.workspace = workspace;
        this.newWorkspaceName = newWorkspaceName;
        this.newComment = newComment;
        this.newWorkingFolders = newWorkingFolders;
        this.newOptions = newOptions;
        this.newLocation = newLocation;
        this.permissionprofile = permissionprofile;
    }

    private static String getName(Workspace workspace)
    {
        String messageFormat = Messages.getString("UpdateWorkspaceCommand.CommandTextFormat");
        return MessageFormat.format(messageFormat, new Object[]{workspace.getName()});
    }

    @Override
    protected boolean runInBackground() {
        return false;
    }

    @Override
    public void run(ProgressIndicator progressIndicator) {
        String messageFormat = Messages.getString("UpdateWorkspaceCommand.ProgressMonitorTextFormat");
        String message = MessageFormat.format(messageFormat, new Object[] { this.workspace.getName() });
        progressIndicator.setText(message);
        progressIndicator.setIndeterminate(false);

        this.workspace.update(this.newWorkspaceName, null, this.newComment, null, this.newWorkingFolders, this.permissionprofile, false, this.newOptions, this.newLocation);

    }

    public Workspace getWorkspace(){
        return this.workspace;
    }

}
